package com.landiao.core.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.landiao.common.weixin.WxMpService;
import com.landiao.common.weixin.bean.WxMpOAuth2AccessToken;
import com.landiao.common.weixin.bean.WxMpUser;
import com.landiao.entities.Vip;
import com.landiao.service.VipService;

public class VipAccreditInterceptor implements HandlerInterceptor {
	
	protected final static Logger log = LoggerFactory.getLogger(VipAccreditInterceptor.class);
	
	@Autowired
	@Qualifier("vipWxService")
	private WxMpService vipWxService;
	
	@Autowired
	private VipService vipService;

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {

	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object obj) throws Exception {
		HttpSession session = request.getSession();
		if(session.getAttribute("wxVipUser") == null){
			if (request.getParameterMap().get("code") != null
					&& !StringUtils.isEmpty(request.getParameterMap().get(
							"code")[0])) {
				String code = request.getParameterMap().get("code")[0];
				WxMpOAuth2AccessToken accessToken = vipWxService.oauth2getAccessToken(code);
				WxMpUser wxUser = vipWxService.userInfo(accessToken.getOpenid(), null);
				session.setAttribute("wxVipUser", wxUser);
				log.debug("wxVipUser:{}",wxUser.toString());
				Vip vip = this.vipService.getByOperId(wxUser.getOpenid());
				if(vip != null){
					session.setAttribute("wxVip", vip);
					return true;
				}
			} else {
				response.sendRedirect(vipWxService.oauth2buildAuthorizationUrl(request.getRequestURL().toString(), "snsapi_base",null));
				return false;
			}
		}else{
			if(session.getAttribute("wxVip")==null){
				WxMpUser wxUser = (WxMpUser)session.getAttribute("wxVipUser");
				Vip vip = this.vipService.getByOperId(wxUser.getOpenid());
				if(vip != null){
					session.setAttribute("wxVip", vip);
					return true;
				}
			}
		}
		if(session.getAttribute("wxVip")==null){//未注册跳转到注册页面
			if(request.getRequestURL().indexOf("vip/add")==-1){
				response.sendRedirect(request.getContextPath() + "/weixin/vip/add?backUrl="+request.getRequestURL());
				return false;
			}
		}else{
			Vip vip = (Vip)session.getAttribute("wxVip");
			vip = this.vipService.getById(vip.getVipId());
			session.setAttribute("wxVip", vip);
			return true;
		}
		return true;
	}
}
